<template>
  <el-dialog :title="dataForm.id? $t('nftgas.gasCreate'): $t('nftgas.gasCreate')" :visible.sync="visible">
    <el-form ref="dataForm" :rules="dataRule" :model="dataForm" label-position="right" label-width="80px">
<!--      <el-form-item label="商户号" prop="mchNo">-->
<!--        <el-input v-model="dataForm.mchNo" />-->
<!--      </el-form-item>-->
      <el-form-item label="次数" prop="nftNum">
        <el-input-number v-model="dataForm.nftNum" :min="0" :step="1" label="次数"/>
      </el-form-item>
      <el-form-item label="渠道" prop="mchNo">
        <el-input v-model="dataForm.mchNo" style="width: 180px" />
      </el-form-item>
      <el-form-item label="原价" prop="nftOriginalPrice">
        <el-input-number v-model="dataForm.nftOriginalPrice" :min="0.01" :precision="2" :step="0.01" label="原价"/>
      </el-form-item>
      <el-form-item label="优惠价" prop="nftSpecialPrice">
        <el-input-number v-model="dataForm.nftSpecialPrice" :min="0.01" :precision="2" :step="0.01" label="优惠价"/>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input-number v-model="dataForm.sort" :min="1" :step="1" label="排序"/>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        {{this.$t('webInfoConfig.cancellation')}}
      </el-button>
      <el-button type="primary" @click="dataFormSubmit()">
        {{this.$t('webInfoConfig.determine')}}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          mchNo: '',
          nftNum: 0,
          sort: 1,
          nftOriginalPrice: 0,
          nftSpecialPrice: 0
        },
        dataRule: {
          nftNum: [
            { required: true, message: '次数不能为空', trigger: 'change' }
          ],
          nftOriginalPrice: [
            { required: true, message: '原价不能为空', trigger: 'change' }
          ],
          nftSpecialPrice: [
            { required: true, message: '优惠价不能为空', trigger: 'change' }
          ],
          mchNo: [
            { required: true, message: '渠道不能为空', trigger: 'change' }
          ]
        },
      }
    },
    methods: {
      init(roleId) {
        this.dataForm.id = roleId || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl('/admin/nftgas/getGasListById'),
                method: 'get',
                params: this.$http.adornParams({id :this.dataForm.id})
              }).then(({ data }) => {
                this.dataForm = data
              })
          }
        })
      },

      // 表单提交
      dataFormSubmit() {
        if(this.dataForm.id){
          this.$http({
            url: this.$http.adornUrl('/admin/nftgas/update'),
            method: this.dataForm.balanceId ? 'put' : 'post',
            data: this.$http.adornData(this.dataForm)
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('remindPop.succeeded'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.isSubmit = false
                this.$emit('refreshDataList')
              }
            })
          })
        }else{
          this.$http({
            url: this.$http.adornUrl('/admin/nftgas/insert'),
            method: this.dataForm.balanceId ? 'put' : 'post',
            data: this.$http.adornData(this.dataForm)
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('remindPop.succeeded'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.isSubmit = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      }
    }
  }
</script>
